home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / russell / gc.lha / interface.c < prev    next >
C/C++ Source or Header  |  1993-03-04  |  3KB  |  87 lines

  1. #include "gc_private.h"
  2. #include <stddef.h>
  3.  
  4. /* These are some additional routines to interface the collector to C     */
  5. /* This is a rather special purpose interface that tries to keep down the */
  6. /* number of collections in the presence of explicit deallocations.      */
  7. /* A call to this malloc effectively declares that the resulting object   */
  8. /* will be explicitly deallocated with very high probability.          */
  9. /* The reduced collection frequency may interfere with object           */
  10. /* coalescing.                                  */
  11. /* If you just want to rename GC_malloc and friends, this is NOT      */
  12. /* the right way to do it.                          */
  13.  
  14. /* This contributed by David Chase (chase@eng.sun.com) a long time      */
  15. /* ago. Much of its original functionality has since been absorbed      */
  16. /* elsewhere.                                  */
  17. /* They illustrates the use of GC_non_gc_bytes                   */
  18. /* Hacked by H. Boehm (11/16/89) to accomodate GC_realloc.                */
  19. /* Further updated (2/20/92) to reflect changes in interfaces and data      */
  20. /* structures.                                  */
  21. /* Further updated (8/25/92) to correct previously introduced bugs and       */
  22. /* make it compile with semi-modern compilers.                  */
  23. /* Note that extern_ptr_t is either void * or char *, as appropriate.     */
  24.  
  25.  
  26. /* This free routine is merely advisory -- it reduces the estimate of
  27.    storage that won't be reclaimed in the next collection, thus
  28.    making it more likely that the collector will run next time more
  29.    memory is needed. */
  30.  
  31. void free(p)
  32. extern_ptr_t p;
  33. {
  34.   size_t inc = GC_size(p);
  35.   GC_non_gc_bytes -= inc;
  36. }
  37.  
  38. /* This free routine adjusts the collector estimates of space in use,
  39.    but also actually releases the memory for reuse.  It is thus "unsafe"
  40.    if the programmer "frees" memory that is actually still in use.  */
  41.  
  42. void unsafe_free(p)
  43. extern_ptr_t p;
  44. {
  45.   size_t inc = GC_size(p);
  46.   GC_non_gc_bytes -= inc;
  47.   GC_free(p);
  48. }
  49.  
  50.  
  51. /* malloc and malloc_atomic are obvious substitutes for the C library
  52.    malloc.  Note that the storage so allocated is regarded as not likely
  53.    to be reclaimed by the collector (until explicitly freed), and thus
  54.    its size is added to non_gc_bytes.
  55. */
  56.  
  57. extern_ptr_t malloc(bytesize)
  58. size_t bytesize;
  59. {
  60.   extern_ptr_t result;
  61.   
  62.   result = (extern_ptr_t) GC_malloc (bytesize);
  63.   GC_non_gc_bytes += (bytesize + 3) & ~3;
  64.   return result;
  65. }
  66.  
  67. extern_ptr_t malloc_atomic(bytesize)
  68. size_t bytesize;
  69. {
  70.   extern_ptr_t result;
  71.   
  72.   result = (extern_ptr_t) GC_malloc_atomic (bytesize);
  73.   GC_non_gc_bytes += (bytesize + 3) & ~3;
  74.   return result;
  75. }
  76.  
  77. extern_ptr_t realloc(old,size)
  78. extern_ptr_t old;
  79. size_t size;
  80. {
  81.   int inc = GC_size(old);
  82.  
  83.   GC_non_gc_bytes += ((size + 3) & ~3) - inc;
  84.   return(GC_realloc(old, size));
  85. }
  86.  
  87.